/**
 *Copyright (c) 2024 watereyes
 * safetymanagement is licensed under Mulan PubL v2.
 * You can use this software according to the terms and conditions of the Mulan
 * PubL v2.
 * You may obtain a copy of Mulan PubL v2 at:
 *          http://license.coscl.org.cn/MulanPubL-2.0
 * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
 * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
 * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
 * See the Mulan PubL v2 for more details.
 */
package com.koron.accident.controller;

import com.koron.accident.domain.InjuriesRegister;
import com.koron.accident.service.api.InjuriesRegisterService;
import com.koron.accident.util.BusinessCode;
import com.koron.common.Excel;
import com.koron.common.StaffAttribute;
import com.koron.common.bean.StaffBean;
import com.koron.common.domain.DataBean;
import com.koron.common.permission.SPIAccountAnno;
import com.koron.common.web.Util;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.io.FileUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import org.swan.bean.MessageBean;
import springfox.documentation.annotations.ApiIgnore;

import java.io.File;
import java.io.InputStream;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Api(tags = "injuriesRegister", description = "工伤登记相关接口")
@RestController
@RequestMapping("/injuriesRegister")
public class InjuriesRegisterController {
private final static Logger logger= LoggerFactory.getLogger(InjuriesRegisterController.class);
    @Autowired
    private InjuriesRegisterService registerService;

    @Autowired
    private Util util;

    @ApiOperation(value = "保存工伤登记")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "id",  dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "corpCode", value = "水司code", required = true, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "staffCode", value = "人员code", required = true, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "staffName", value = "人员名称", required = true, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "departmentCode", value = "人员所在单位code", required = true, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "departmentName", value = "人员所在单位名称", required = true, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "accidentBulletinId", value = "事故快报id", required = true, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "accidentBulletinName", value = "事故快报名称", required = true, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "injuredPart", value = "受伤部位",  dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "injuredLevel", value = "严重程度，0轻微，1严重，3致命",  dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "sex", value = "性别",  dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "birthday", value = "出生日期",  dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "staffPhone", value = "个人联系电话",  dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "staffSocialInsuranceNum", value = "个人参保号",  dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "departmentPhone", value = "单位联系电话",  dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "deptSocialInsuranceNum", value = "单位参保编号",  dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "workTime", value = "参加工作时间",  dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "diagnosticTime", value = "诊断时间",  dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "occupationalDiseases", value = "职业病名称",  dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "beginTime", value = "接触职业病开始时间",  dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "endTime", value = "接触职业病结束时间",  dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "visitingTimes", value = "探望人次",  dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "chaperone", value = "陪护人",  dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "chaperoneDay", value = "陪护人天",  dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "isContact", value = "是否接触职业病，0否，1是",  dataType = "Integer", paramType = "query"),
            @ApiImplicitParam(name = "chaperoneSituation", value = "陪护情况",  dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "directCost", value = "直接费用",  dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "indirectCost", value = "间接费用",  dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "otherCost", value = "其他费用",  dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "injureContent", value = "受伤经过",  dataType = "String", paramType = "query"),
    }
    )
    @RequestMapping(value = "/save.htm", method = RequestMethod.POST)
    public MessageBean<Integer> save(@SPIAccountAnno @StaffAttribute("_user") @ApiIgnore StaffBean user, String userCode, @ApiIgnore InjuriesRegister bean,
                                     @RequestParam(required = false) final String keepFiles, @RequestParam(required = false) final String delFiles,
                                     @RequestParam(required = false) final String pdfKeepFiles, @RequestParam(required = false) final String pdfDelFiles,
                                     @RequestParam(required = false) final MultipartFile[] files) {
        if (user == null) {
            List<StaffBean> staffBeanList = (List<StaffBean>) util.staff(userCode, null).getData();
            if (null != staffBeanList && staffBeanList.size() > 0)
                user = staffBeanList.get(0);
        }
        if (null == user) {
            MessageBean<Integer> messageBean = new MessageBean<>();
            messageBean.setCode(BusinessCode.NOT_LONGIN.getCode());
            messageBean.setDescription(BusinessCode.NOT_LONGIN.getDescription());
            return messageBean;
        }
        bean.setUpdateTime(new Date());
        bean.setUpdateUser(user.getCode());
        bean.setUpdateUserName(user.getName());
        return registerService.save(bean, keepFiles, delFiles, pdfKeepFiles, pdfDelFiles, files);
    }

    @ApiOperation("删除工伤登记接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "工伤登记id", required = true, dataType = "String", paramType = "query"),
    })
    @RequestMapping(value = "/delete.htm", method = RequestMethod.POST)
    public MessageBean<Integer> delete(@RequestParam final String id) {
        return registerService.delete(id);
    }

    @ApiOperation("查询单个工伤登记快报接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "事故id", required = true, dataType = "String", paramType = "query"),
    })
    @RequestMapping(value = "/details.htm", method = RequestMethod.POST)
    public MessageBean<InjuriesRegister> details(@RequestParam final String id) {
        return registerService.queryOne(id);
    }

    @ApiOperation("事故报告列表接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "corpCode", value = "水司code",  dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "staffName", value = "人员名称",  dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "departmentCode", value = "事发单位",  dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "page", value = "当前页", required = true, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "pageSize", value = "每页条数", required = true, dataType = "int", paramType = "query"),
    })
    @RequestMapping(value = "/list.htm", method = RequestMethod.POST)
    public MessageBean<DataBean<InjuriesRegister>> list(@RequestParam(required = false) final String corpCode, @RequestParam(required = false) final String staffName, @RequestParam(required = false) final String departmentCode,
                                                        @RequestParam Integer page, @RequestParam Integer pageSize) {
        Integer start = null;
        if (page == null || page <= 0) {
            page = 1;
            if (pageSize == null || pageSize <= 0) {
                pageSize = 15;
            }
        }
        start = (page <= 1) ? 0 : (page - 1) * pageSize;
        MessageBean<DataBean<InjuriesRegister>> msg = registerService.list(corpCode, staffName, departmentCode, start, pageSize);
        if (null != msg.getData())
            msg.getData().setPageNo(page);
        return msg;
    }


    @ApiOperation("工伤管理列表导出接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "corpCode", value = "水司code",  dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "staffName", value = "人员名称",  dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "departmentCode", value = "事发单位",  dataType = "String", paramType = "query"),
    })
    @RequestMapping(value = "/exportList.htm", method = RequestMethod.GET)
    public HttpEntity<?> exportList(@SPIAccountAnno @StaffAttribute("_user") @ApiIgnore StaffBean user, String corpCode,
                                    String staffName, String departmentCode) {
        if (null == user) {
            return null;
        }
        List<InjuriesRegister> list = registerService.exportList(user, corpCode, staffName, departmentCode);
        Map<String, Object> map = new HashMap<>();
        map.put("data", list);
        File targetFile = new File("static/excel/injuriesRegisterList.xlsx");
        try {
            InputStream in = getClass().getClassLoader().getResourceAsStream("static/excel/injuriesRegisterList.xlsx");
            FileUtils.copyInputStreamToFile(in, targetFile);
        } catch (Exception e) {
            logger.error(e.getMessage(),e);
        }
        return Excel.createFile("工伤管理列表", targetFile, "injuriesRegisterList.btl", map);
    }
}
